﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Pavilion.DesignerTool
{
    public class TrackedLoadNode : TrackedNodeBase
    {
        private LoadNode originalNode, loadNode;

        public TrackedLoadNode(LoadNode loadNode, NodeScript parentNodescript, ResourceItem parentResource) : base(loadNode, parentNodescript, parentResource)
        {
            this.loadNode = loadNode;
            originalNode = loadNode.Clone() as LoadNode;
        }

        public override void TransferDataUpdate()
        {
            base.TransferDataUpdate();

            CollectionDelta<string> gameItemDelta = new CollectionDelta<string>(originalNode.GameItemCollection, loadNode.GameItemCollection);

            foreach (string deletedGameItemName in gameItemDelta.DeletedItems)
                GameItemTransfer.Remove(ParentResource.Name, ParentNodeScript.Name, loadNode, deletedGameItemName);

            foreach (string newGameItemName in gameItemDelta.NewItems)
                GameItemTransfer.Add(ParentResource.Name, ParentNodeScript.Name, loadNode, newGameItemName);

            foreach (string identicalGameItemName in gameItemDelta.IdenticalItems)
                TransferDataUpdateGameItem(identicalGameItemName);
        }

        private void TransferDataUpdateGameItem(string gameItemName)
        {
            CollectionDelta<GameData> gameToolDataDelta = new CollectionDelta<GameData>(originalNode.ObtainGameTools(gameItemName), loadNode.ObtainGameTools(gameItemName),
                (original, current) => original.GameToolName == current.GameToolName);

            foreach (GameData deletedGameToolData in gameToolDataDelta.DeletedItems)
                GameToolTransfer.Remove(ParentResource.Name, ParentNodeScript.Name, loadNode, deletedGameToolData.GameItemName, deletedGameToolData.GameToolName);

            foreach (GameData newGameToolData in gameToolDataDelta.NewItems)
                GameToolTransfer.Add(ParentResource.Name, ParentNodeScript.Name, loadNode, newGameToolData);

            foreach (GameData identicalGameToolData in gameToolDataDelta.IdenticalItems)
                TransferDataUpdateGameTool(identicalGameToolData);
        }

        private void TransferDataUpdateGameTool(GameData gameToolData)
        {
            GameData originalGameToolData = originalNode.ObtainGameData(gameToolData.GameItemName, gameToolData.GameToolName);
            CollectionDelta<string> collectionDelta = new CollectionDelta<string>(originalGameToolData.PropertyDictionary.Keys, gameToolData.PropertyDictionary.Keys);

            foreach (string deletedProperty in collectionDelta.DeletedItems)
                GameToolTransfer.RemoveGameToolProperty(ParentResource.Name, ParentNodeScript.Name, loadNode, gameToolData, deletedProperty);

            foreach (string newProperty in collectionDelta.NewItems)
                GameToolTransfer.AddGameToolProperty(ParentResource.Name, ParentNodeScript.Name, loadNode, gameToolData, newProperty);

            foreach (string identicalProperty in collectionDelta.IdenticalItems)
            {
                if (originalGameToolData.PropertyDictionary[identicalProperty] != gameToolData.PropertyDictionary[identicalProperty])
                    GameToolTransfer.UpdateGameToolProperty(ParentResource.Name, ParentNodeScript.Name, loadNode, gameToolData, identicalProperty);
            }
        }
    }
}
